package com.woniuxy.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.param.BaseParam;
import com.woniuxy.param.PageQueryParam;

import java.util.Map;


/**
 * 分页的工具类
 *
 * @author 周川雄
 * @date 2022/1/3 20:38
 */
public class PageMysqlUtil {


    public static <E> QueryWrapper<E> getLikeWrapper(Class<E> modelClass, Object o) {

        if(o == null) {
            return null;
        }

        QueryWrapper<E> wrapper = new QueryWrapper<>();
        Map<String, String> map = Object2MapUtil.object2Map(o);

        for (String key : map.keySet()) {
            if(map.get(key) != null)
                wrapper.like(key, map.get(key));
        }

        return wrapper;
    }






    /**
     * mysql分页查询
     *
     * @param dto      具体实体类dto的类型
     * @param iService 操作数据库对应的service
     * @param <T>      泛型
     * @return BaseDto<T>   返回dto，包含当前页，页数，具体实体类dto集合
     */
    public static <T, E> BaseDto<T> pageQuery(Class<T> dto, IService iService, QueryWrapper<E> queryWrapper, BaseParam param) {

//        PageQueryParam param = new PageQueryParam();
//
        //设置默认值
        if (param.getIndexPage() == null) {
            param.setIndexPage(1L);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(3L);
        }


        Page<T> page;
        if (queryWrapper == null) {
            page = (Page<T>) iService.page(new Page<>(param.getIndexPage(), param.getPageSize()));
        } else {
            page = (Page<T>) iService.page(new Page<>(param.getIndexPage(), param.getPageSize()), queryWrapper);
        }

        BaseDto<T> baseDto = new BaseDto<>();
        baseDto.setIndexPage(page.getCurrent());
        baseDto.setPageCount(page.getPages());
        //判断是否有上一页
        baseDto.setHasPrevious(page.hasPrevious());
        //判断是否有下一页
        baseDto.setHasNext(page.hasNext());
        baseDto.setTotal(page.getTotal());

        baseDto.setList(WoniuBeanUtils.copyList(page.getRecords(), dto));
        return baseDto;
    }




    /**
     * mysql分页查询
     * @param dto  具体实体类dto的类型
     * @param iService  操作数据库对应的service
     * @param <T>       泛型
     * @return BaseDto<T>   返回dto，包含当前页，页数，具体实体类dto集合
     */
    public static <T,E> BaseDto<T> pageQuery(Class<T> dto, IService iService, QueryWrapper<E> queryWrapper) {

        PageQueryParam param = new PageQueryParam();

        //设置默认值
        if (param.getIndexPage() == null) {
            param.setIndexPage(1L);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(3L);
        }


        IPage page;
        if(queryWrapper == null) {
            page = iService.page(new Page(param.getIndexPage(), param.getPageSize()));
        }else {
            page = iService.page(new Page(param.getIndexPage(), param.getPageSize()), queryWrapper);
        }

        BaseDto<T> baseDto = new BaseDto<>();
        baseDto.setIndexPage(page.getCurrent());
        baseDto.setPageCount(page.getPages());
        baseDto.setList(WoniuBeanUtils.copyList(page.getRecords(), dto));
        return baseDto;
    }
}